#include "edituserdialog.h"
#include "ui_edituserdialog.h"
#include "vidhub.h"
#include "toast.h"

EditUserDialog::EditUserDialog(QWidget *parent, const QString& text, model::AdminInfo& adminInfo)
    : QDialog(parent)
    , ui(new Ui::EditUserDialog)
    , adminInfo(adminInfo)
{
    ui->setupUi(this);
    auto instance = VidHub::getInstance();
    this->move(instance->mapToGlobal(QPoint(0,0)));
    // 去除边框 & 窗口透明
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 设置ui界面标题
    ui->titleLabel->setText(text);

    // 对手机号编辑框进行限制
    QRegularExpression regExp("^1\\d{10}$");
    QValidator* validator = new QRegularExpressionValidator(regExp, this);
    ui->phoneEdit->setValidator(validator);

    //设置用户角色
    ui->roleComboBox->addItem("平台管理员");
    ui->roleComboBox->setCurrentIndex(0);

    // 设置管理员信息
    if(!adminInfo.userId.isEmpty()){
        // 编辑管理员信息
        ui->phoneEdit->setText(adminInfo.phoneNumber);
        // 目前角色只有一个
        if(model::RoleType::Admin == adminInfo.roleType){
            ui->roleComboBox->setCurrentIndex(0);
        }
        ui->nameEdit->setText(adminInfo.nickname);
        if(model::eable == adminInfo.userStatu){
            ui->startRadioBtn->setChecked(true);
        }else{
            ui->stopRadioBtn->setChecked(true);
        }
        ui->commentTextEdit->setPlainText(adminInfo.userMemo);
    }

    // 连接信号槽
    connect(ui->cancelBtn, &QPushButton::clicked, this, &EditUserDialog::onCancelBtnClicked);
    connect(ui->submitBtn, &QPushButton::clicked, this, &EditUserDialog::onSubmitBtnClicked);
    connect(ui->commentTextEdit, &QPlainTextEdit::textChanged, this, [=](){
        QString text = ui->commentTextEdit->toPlainText();
        if(text.size() > 10) {
            ui->commentTextEdit->setPlainText(text.mid(0,10));
        } else {
            QTextCursor textCursor = ui->commentTextEdit->textCursor();
            textCursor.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
            ui->commentTextEdit->setTextCursor(textCursor);
            ui->wordCount->setText(QString::number(text.size()) + "/10");
        }
    });
}

EditUserDialog::~EditUserDialog()
{
    delete ui;
}

bool EditUserDialog::getCommitResult() const
{
    return isCommit;
}

void EditUserDialog::setPhoneEditReadOnly(bool isReadOnly)
{
    ui->phoneEdit->setReadOnly(isReadOnly);
}

void EditUserDialog::onSubmitBtnClicked()
{
    // 当用户点击提交时，将新增或编辑的管理员信息收集 adminInfo
    adminInfo.phoneNumber = ui->phoneEdit->text();

    // 角色
    adminInfo.roleType = model::RoleType::Admin;
    // 昵称
    adminInfo.nickname = ui->nameEdit->text();
    // 状态
    model::AdminStatus adminStatus = model::AdminStatus::eable;
    if(ui->stopRadioBtn->isChecked()){
        adminStatus = model::AdminStatus::disable;
    }

    // 注意：禁止管理员自己修改自己
    auto dataCenter = model::DataCenter::getInstance();
    auto myselfInfo = dataCenter->getMyselfInfo();
    if(myselfInfo->userId == adminInfo.userId && adminStatus != adminInfo.userStatu){
        Toast::showMessage("禁止管理员启用或禁用自己");
        return;
    }

    adminInfo.userStatu = adminStatus;
    adminInfo.userMemo = ui->commentTextEdit->toPlainText();
    isCommit = true;
    close();
}

void EditUserDialog::onCancelBtnClicked()
{
    isCommit = false;
    this->close();
}
